package com.neusmart.common.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.neusmart.common.R;

public class ClearEditText extends EditText {

    public interface OnTextChangedListener {
        public void onTextChanged(boolean isEmpty);
    }

    private Drawable imgCloseButton = getResources().getDrawable(
            R.mipmap.ic_clear_image);

    private OnTextChangedListener l;

    public ClearEditText(Context context) {
        super(context);
        init();
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    void init() {
        // Set bounds of the Clear button so it will look ok
        imgCloseButton.setBounds(0, 0, imgCloseButton.getIntrinsicWidth(),
                imgCloseButton.getIntrinsicHeight());

        // There may be initial text in the field, so we may need to display the
        // button
        handleClearButton();

        // if the Close image is displayed and the user remove his finger from
        // the button, clear it. Otherwise do nothing
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                ClearEditText et = ClearEditText.this;

                if (et.getCompoundDrawables()[2] == null)
                    return false;

                if (event.getAction() != MotionEvent.ACTION_UP)
                    return false;

                if (event.getX() > et.getWidth() - et.getPaddingRight()
                        - imgCloseButton.getIntrinsicWidth()) {
                    et.setText("");
                    ClearEditText.this.handleClearButton();
                }
                return false;
            }
        });

        // if text changes, take care of the button
        addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (l != null)
                    l.onTextChanged(getText().toString().equals(""));
                ClearEditText.this.handleClearButton();
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

        });
    }

    void handleClearButton() {
        if (getText().toString().equals("")) {
            // remove the clear button
            setCompoundDrawables(getCompoundDrawables()[0],
                    getCompoundDrawables()[1], null, getCompoundDrawables()[3]);
        } else {
            // add clear button
            setCompoundDrawables(getCompoundDrawables()[0],
                    getCompoundDrawables()[1], imgCloseButton,
                    getCompoundDrawables()[3]);
        }
    }

    public void setOnTextChangedListener(OnTextChangedListener l) {
        this.l = l;
    }

}
